package oad.a233.v1.control;

import java.io.InputStream;



import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class OADRollInfoView extends View {
	
	private int _PointCount=0;
	private Bitmap _ImgPointDefault;
	private Bitmap _ImgPointSelect;
	final private int  _ViewHeight = 14;
	final private int _PointSpace = 5;
	private int _PointTotalWidth;
	private int _PointBeginPosX;
	private int _PointBeginPosY;
	private boolean _IsCaluPos;
	private int _ViewIndex =0 ;
	/**
	 * @param _PointCount the _PointCount to set
	 */
	public void set_PointCount(int _PointCount) {
		this._PointCount = _PointCount;		

		_IsCaluPos = false;		
	}
	

	/**
	 * @param _ViewIndex the _ViewIndex to set
	 */
	public void set_ViewIndex(int _ViewIndex) {
		this._ViewIndex = _ViewIndex;
		
	}


	
	
	public OADRollInfoView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		InitView();
	
	}
	
	private void CaluPointPos()
	{
		if(!_IsCaluPos && this.getMeasuredWidth()>0)
		{
			_PointTotalWidth = _PointCount * _ImgPointDefault.getWidth()+(_PointCount-1) * _PointSpace;
			//has a bug,when point is too many.
			_PointBeginPosX = (this.getMeasuredWidth()-_PointTotalWidth)/2;
			_PointBeginPosY =  (_ViewHeight - _ImgPointDefault.getHeight())/2;
			
			_IsCaluPos = true;
		}
	}
	
	private void InitView()
	{
		InputStream is = this.getResources().openRawResource(R.drawable.point_default_2);  
		_ImgPointDefault = BitmapFactory.decodeStream(is);	
		
		is = this.getResources().openRawResource(R.drawable.point_select_1);  
		_ImgPointSelect = BitmapFactory.decodeStream(is);
		
	}



	@Override
	protected void onDraw(Canvas c) {
		// TODO Auto-generated method stub
		this.CaluPointPos();
		
		c.drawColor(Color.parseColor("#88FFFFFF"));
		if(_PointCount>0)
		{
			Paint paint = new Paint();
			
			int l = _PointBeginPosX;
			int h = _PointBeginPosY;
			for(int i=0;i<_PointCount;i++)
			{
				c.drawBitmap(_ImgPointDefault, l, h,paint);
			
				if(i == _ViewIndex)
				{
					c.drawBitmap(_ImgPointSelect, l, h,paint);
				}
				
				l+=_ImgPointDefault.getWidth()+_PointSpace;	
			}
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		heightMeasureSpec = MeasureSpec.makeMeasureSpec(_ViewHeight, MeasureSpec.EXACTLY);
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		
		
	}




	
	

}
